﻿#include <iostream>
#include <string.h>
using namespace std;



//ham tra ve mot cum tu , tu vi tri i den dau cach
char* LayCumTu(char chuoi[], int i);


//dich chuyen xau
void DichChuyen(char chuoi[], int vt, int gioihan, int honkem, int huong);

//ham hoan doi hai xau
void HoanDoi(char chuoi[], char xau1[], char xau2[], int vt_xau1, int vt_xau2);

//tim mot xau trong chuoi ban dau
//Neu co return ve vị trí đầu tiên mà xâu đó xuất hiện
int TimXau(char chuoi[], char xau[]);


//xoa mot xau trong chuoi
void XoaXau(char chuoi[], char xau[]);

//Them xau vao chuoi
void ThemXau(char chuoi[],char xau[], int vt_them);

//doi chuoi 'meo' voi 'cho1'
int main()
{
	char n[100] ;
	char xau1[100];
	char xau2[100];
	int vt_xau1, vt_xau2;
	cout << "Nhap chuoi ban dau: ";
	fflush(stdin);
	cin.getline(n, 100);

	cout << "Nhap xau can them: ";
	gets_s(xau1);
	cout << "Nhap vi tri them: ";
	cin >> vt_xau1;
	ThemXau(n, xau1, vt_xau1);
	puts(n);
	return 0;
}

char* LayCumTu(char chuoi[], int i)
{
	char *tam;
	tam = new char[strlen(chuoi)+1];
	int j = 0;
	while (chuoi[i] != ' ' && chuoi[i] != '\0') tam[j++] = chuoi[i++];
	tam[j] = '\0';
	return tam;
}


//dich chuyen xau
void DichChuyen(char chuoi[], int vt, int gioihan, int honkem, int huong)
//tham so truyen vao: 1. chuoi: chuoi ban dau, 
//					  2. vt: vi tri bat dau dich chuyen
//					  3. gioihan: vi tri ket thuc dich chuyen
//					  4. honkem: khoang cach giua gia tri ban dau va gia tri can dich chuyen toi
//					  5. huong: huong dich chuyen qua trai(huong > 0) hoac phai(huong < 0).	
{
	if (huong > 0)
		while (vt + honkem <= gioihan)
		{
			if(chuoi[vt + honkem] != '\0') chuoi[vt] = chuoi[vt + honkem];
			++vt;

		}
	else 
		while (vt + honkem >= gioihan)
		{
			chuoi[vt] = chuoi[vt + honkem];
			--vt;
		};
}


//ham hoan doi hai xau
void HoanDoi(char chuoi[], char xau1[], char xau2[], int vt_xau1, int vt_xau2)
{
	int dd_xau1 = strlen(xau1), dd_xau2 = strlen(xau2);
	int huong = dd_xau1 - dd_xau2;
	if (huong > 0) DichChuyen(chuoi, vt_xau1 + (dd_xau1 - dd_xau2) + 1,vt_xau2 + strlen(xau2), dd_xau1 - dd_xau2, huong);//tien ve (trai)
	if (huong < 0) DichChuyen(chuoi, vt_xau2 + (dd_xau2 - dd_xau1) - 1, vt_xau1 + strlen(xau1), dd_xau1 - dd_xau2, huong);//tien ve (phai)

	//dua xau 2 vao xau1
	int j = 0;
	for (int i = vt_xau1; i <= vt_xau1 + dd_xau2 - 1; i++)
	{
		chuoi[i] = xau2[j];
		++j;
	}
	//dua xau1 vao xau2
	j = 0;
	for (size_t i = vt_xau2 - (huong); i < vt_xau2 -huong + strlen(xau1); i++)
	{
		chuoi[i] = xau1[j];
		++j;
	}
}

int TimXau(char chuoi[],char xau[])
{
	//ham tra ve gia tri -1 neu khong tim thay xau trong chuoi
	//nguoc lai tra ve vi tri cua xau 
	for (size_t i = 0; i < strlen(chuoi); i++)
	{
		if (chuoi[i] == xau[0])
		{
			//gia su ban dau day la vi tri can tim f= 1
			// neu f = 0 thi day khong phai la vi tri can tim
			int f = 1;
			for (size_t j = 1; j < strlen(xau); j++)
				if (chuoi[i + j] != xau[j])
				{
					f = 0;
					break;
				}
			if (f == 1) return i;
		}
	}
	return -1;
}

void XoaXau(char chuoi[], char xau[])
{
	int vt_xau = TimXau(chuoi,xau);
	int dd_xau = strlen(xau);
	if (vt_xau == -1)
		cout << "Khong tim thay xau trong chuoi!\n";
	else
	{
		DichChuyen(chuoi, vt_xau, strlen(chuoi), dd_xau + 1, 1);
		chuoi[strlen(chuoi) - dd_xau] = '\0';
	}
}


void ThemXau(char chuoi[], char xau[], int vt_them)
{
	int dd_xau = strlen(xau);
	int dd_chuoi = strlen(chuoi);
	chuoi[dd_chuoi] = ' ';
	if (vt_them < dd_chuoi)
	{
		xau[dd_xau] = '\0';
		xau[dd_xau] = ' ';
		dd_xau += 1;
	}
	else 
	{
		++vt_them;
	}
	chuoi[dd_chuoi + dd_xau + 1] = '\0';
	DichChuyen(chuoi, dd_chuoi + dd_xau, vt_them, -dd_xau, -1);
	for (int i = 0; i < dd_xau; i++)
	{
		chuoi[vt_them + i] = xau[i];
	}
	
}